'use strict';

const Service = require('egg').Service;
  
const { isBlankList } = require('../utils/object');

class ExpertService extends Service {

  newExpertCollect({ name, parentId, isParent, sort }) {
    const ec = new this.ctx.model.ExpertCollect();

    ec.name = name;
    ec.parent_id = parentId || '';
    ec.is_parent = isParent || false;
    ec.ecsort = sort;

    ec.save();
    return [ ec._id ];
  }

  async getExpertCollectList({ parentId }) {
    const query = { deleted: false, is_parent: false, parent_id: parentId };
    const option = { sort: '+sort' };
    const list = await this.ctx.model.ExpertCollect.find(query, [], option).exec();
    if (!isBlankList(list)) {
      return [ ];
    }
    return [ list ];
  }

  async getExpertParentCollectList() {
    const query = { deleted: false, is_parent: true };
    const option = { sort: '+sort' };
    const list = await this.ctx.model.ExpertCollect.find(query, [], option).lean();
    if (!isBlankList(list)) {
      return [ ];
    }

    const fulllist = await Promise.all(
      list.map(async (item) => {
        console.log('parent_id: ', item.parent_id);
        const [ cList ] = await this.ctx.service.expert.getExpertCollectList({
          parentId: item._id,
        });
        if (isBlankList(cList)) {
          item.childNode = cList;
        } else {
          item.childNode = [];
          item.childNode.push({
            name: '全部',
            _id: 'all',
          });
        }

        
        return item;
      })
    );
    return [ fulllist ];
  }

  async getLightExpertDetail({ expertId }) {
    const query = { _id: expertId, deleted: false };

    const expert = await this.ctx.model.Expert.findOne(
        query,
        ['name', 'categray', 'shanchang', 'physician', 'avatar_image', 'extend'],
      );

    if (!expert) {
      return [];
    }

    return [ expert ]
  }

  async getExpertFollowDetail({ expertId, userId }) {
        
    const followQuery = { expert_id: expertId, user_id: userId, };
    
    const expertFollow = await this.ctx.model.ExpertFollow.findOne(followQuery, ['follow']);

    return [ expertFollow ];
  }

  async getFullExpertDetail({ expertId }) {

    const query = { _id: expertId, deleted: false };
    const expert = await this.ctx.model.Expert.findOne(query);

    if (!expert) {
      return [];
    }

    const [ evaluateList, totalCount ] = await this.ctx.service.expertEvaluation.getEvaluationListByQuery({
      expertId,
      pageNum: 1,
      pageSize: 5,
    });
    
    const followQuery = { expert_id: expertId };
    
    const expertFollow = await this.ctx.model.ExpertFollow.findOne(followQuery, ['follow']);

    return [ expert, evaluateList, totalCount, expertFollow ];
  }


  getCountByQuery(query) {
    query.deleted = false;
    return this.ctx.model.Expert.count(query).exec();
  }

  async getExpertList(query = {}, option = {}) {
    query.deleted = false;

    let experts = await this.ctx.model.Expert.find(
      query,
      [],
      option
    ).lean();

    experts = experts.map(item => {
      item.expert_id = item._id;
      return item;
    })

    if (experts.length === 0) {
      return [];
    }

    return experts;

  }

  newAndSave(obj) {
    const expert = new this.ctx.model.Expert();

    for(var key in obj) {
      expert[key] = obj[key];
    }
    
    return expert.save();
  }
}

module.exports = ExpertService;
